//--------------------------------------------------------------------------------------
// File: vantagi.fx
//
// The effect file for the vantagi sample.  
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

float4 lightDir : Direction

<
	string Object = "DirectionalLight";
    string Space = "World";
> = {0.0f, 0.0f, -1.0f, 0.0f};

float4 lightColor : Diffuse
<
    string UIName = "Diffuse Light Color";
    string Object = "DirectionalLight";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float4 lightAmbient : Ambient
<
    string UIName = "Ambient Light Color";
    string Space = "material";
> = {0.0f, 0.0f, 0.0f, 1.0f};

float4 materialDiffuse : Diffuse
<
    string UIName = "Surface Color";
    string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float4 materialSpecular : Specular
<
	string UIName = "Surface Specular";
	string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float shininess : SpecularPower
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName = "specular power";
> = 40.0;

float power
<
    string UIWidget = "slider";
    float UIMin = 0.01;
    float UIMax = 2.0;
    float UIStep = 0.01;
    string UIName = "power";
> = 1;

//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4x4 g_mWorldViewProjection		: WorldViewProjection;
float4x4 g_mWorldInverseTranspose	: WorldInverseTranspose;

texture Texture : Diffuse
<
	string ResourceName = "default_color.dds";
>;

sampler TextureSampler = 
sampler_state
{
    Texture = <Texture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

//------------------------------------
struct VS_INPUT {
	float3 position					: POSITION;
	float3 normal						: NORMAL;
	float4 texCoord					: TEXCOORD0;
};

struct VS_OUTPUT {
	float4 hPosition					: POSITION;
	float4 texCoord					: TEXCOORD0;
	float3 normal						: TEXCOORD1;
	float4 Diff						: COLOR0;
	float4 Spec						: COLOR1;
};

//------------------------------------
VS_OUTPUT VS(VS_INPUT IN) 
{
    VS_OUTPUT OUT;
	OUT.hPosition = mul( float4(IN.position.xyz , 1.0) , g_mWorldViewProjection);
	OUT.texCoord = IN.texCoord;	
	OUT.normal = IN.normal;

	return OUT;
}

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};

PS_OUTPUT PS( VS_OUTPUT In ) 
{ 
	PS_OUTPUT Output;

	Output.RGBColor = tex2D(TextureSampler, In.texCoord);

	if( Output.RGBColor.a !=0 )
	{
//		float3 N = normalize( mul(In.normal, g_mWorldInverseTranspose).xyz );
		float3 N = In.normal;
		float3 L = normalize( -lightDir.xyz );
		float3 C = normalize( cross( L, float3( 0, 1, 0 ) ) );
	
		float diff = max(0, dot( N, L ) );
	
		float  spec = pow( cos( dot( N, C ) ), shininess ) * power;
		float4 specColor = materialSpecular * lightColor * spec * diff;

		Output.RGBColor = (Output.RGBColor * In.Diff + specColor);//*(1-Output.RGBColor.a);
//		Output.RGBColor.a=1;
	}
	else
	{
		Output.RGBColor = Output.RGBColor * In.Diff + In.Spec;
	}
	return Output;
}



//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique RenderScene
{
    pass P0
    {
//		VertexShader	= compile vs_1_1 VS();
		PixelShader		= compile ps_2_0 PS();
    }
}
